<?php
/**
* author: lijj
* time：2016-3-15
* function: register changeInfo　
*/
class IndexAction extends Action {
    public function index() {
        $this->display();
    }

    public function register(){
        $TEACHERINFO = D('Teacher/TeacherInfo');
        $openId = I('openId');
        session('openId',$openId);
        if(!strlen($openId))
            $this->error("请在微信端注册");
        if($TEACHERINFO->where('openId = "'.$openId.'"')->find())
            $this->error("你已经注册过了，请联系平台管理员");
        $this->assign('signPackage',$signPackage);
        $this->display();
    }

    public function handleTeacherInfo(){
        if(!IS_AJAX) 
            $this->error('你输入的网址不存在');

        /*==========定义变量============*/
        $TEACHERINFO = D('Teacher/TeacherInfo');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取改页面');
        if($TEACHERINFO->create()){
            $teacherInfo = array(
                'openId' => $openId,
                'name' => I('name'),
                'telphoneNum' => I('telphoneNum'),
                'telphoneShortNum' => I('telphoneShortNum'),
                'email' => I('email'),
                'qq' => I('qq')
                );
            //说明这是修改信息的判别条件
            if($TEACHERINFO->where('id = '.I("id").' AND openId = "'.$openId.'"')->find()){
            // if(false){
                $teacherInfo['password'] = I('id');
                /*=============更改数据库============*/
                if($TEACHERINFO->where('id = '.I("id").' AND openId = "'.$openId.'"')->save($teacherInfo)){
                    $this->ajaxReturn(array('status' => 1),'json');
                }else{
                    $this->ajaxReturn( array('status' => 0),'json');    
                }
            }else{
                 /*=============插入数据库============*/
                if($TEACHERINFO->data($teacherInfo)->add()){
                    $this->ajaxReturn(array('status' => 1),'json');
                }else{
                    $this->ajaxReturn( array('status' => 0),'json');    
                }
            }
        }else{
            $this->ajaxReturn( array('status' => 1),'json');    
        }
    }

    public function changeInfo(){
        /*=======定义一些变量=======*/
        $TEACHERINFO = D('Teacher/TeacherInfo');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取改页面');
        $info = $TEACHERINFO->where("openId='".$openId."'")->find();
        $this->assign('info',$info);
        $this->display();
    }
    public function myClass(){
        /*=========定义变量=============*/
        $TEACHER = D('Teacher/TeacherInfo');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取改页面');
        $TEACLASS = D('Teacher/TeacherClasses');
        $teaClass = $TEACLASS->where('openId = "'.$openId.'"')->select();
        $this->assign('teaClass',$teaClass);
        // p($teaClass);
        $this->display();
    }

    public function AddMyClass(){
        if(!IS_AJAX)
            $this->error('你输入的网址不存在');

        /*==========定义变量============*/
        $TEACHERCLASS = D('Teacher/TeacherClasses');
        $TEACHER = D('Teacher/TeacherInfo');
        $openId = session('?openId') ? session('openId') : $this->error('请重新获取该页面');
        $name = $TEACHER->getName($openId);
        if($TEACHERCLASS->create()){
            $classes = array(
                'openId' => $openId,
                'name' => $name,
                'class' => I('classes'),
                'college' => I('college'),
                'school' => I('school'),
                'semester' => I('semester'),
                'schooltime' => I('schooltime'),
                'place' => I('place'),
                'time' => date("Y-m-d H:i:s")
                );
            if($TEACHERCLASS->data($classes)->add())
                $this->ajaxReturn(array('status' => 1),'json');
            else
                $this->ajaxReturn(array('status' => 0),'json');
        }else{
            $this->ajaxReturn( array('status' => 1),'json'); 
        }
    }
    
    //Counter测试
    public function counter() {
        $c = new SaeCounter(); //实例化
        $c->create("test"); //创建计算器
        $c->set("test", 30); //设置值
        $ret = $c->get("test"); //获得值
        dump($ret);
        $ret = $c->incr("test"); //增加值
        dump($ret);
        $ret = $c->decr("test"); //减少值
        dump($ret);
    }

    //KVDB测试
    public function kv() {
        $k = new SaeKV();
        $k->init();
        $k->set('a', 'aaa'); //建立一条字符串数据
        $ret = $k->get('a'); //获得a的值
        dump($ret);
        $k->set('b', array('a', 'b', 'c')); //可存储数组或对象
        $ret = $k->get("b"); //获得b的值
        dump($ret);
        $k->delete("a"); //删除a
    }

    //rank排行榜测试
    public function rank() {
        $r = new SaeRank();
        $r->create("list", 100); //创建一个榜单。
        $r->set("list", "a", 3); //设置值
        $r->set("list", "b", 4);
        $r->set("list", "c", 1);
        $r->increase("list", "c"); //增加值
        $ret = $r->getList("list", true); //获得排行榜
        dump($ret);
        $ret = $r->getRank("list", "a"); //获得某个键的排名,注意是从0开始
        dump($ret);
        $r->clear("list"); //清空排行榜
    }

}

?>